package com.war.demo.service.service.impl;

//import com.alibaba.druid.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.war.demo.common.dao.PasswordMapper;
import com.war.demo.common.dao.UserMapper;
import com.war.demo.service.enmu.ApiResponse;
import com.war.demo.service.enmu.ResultCode;
import com.war.demo.service.dao.Password;
import com.war.demo.service.dao.User;
import com.war.demo.service.service.LoginService;
import com.war.demo.service.util.JwtUtil;
import com.war.demo.service.util.LoggerUtil;
import com.war.demo.service.vo.UserVo;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class LoginServiceimpl implements LoginService {

    @Autowired
    private UserMapper userMapper;//与dao层进行交互

    @Autowired
    private PasswordMapper passwordMapper;

//    @Autowired
//    private RedisUtil redisUtil;

    @Override
    public ApiResponse verifyLogin(String telphone, String pad) {

        ApiResponse apiResponse = new ApiResponse();

        /**
         * 根据用户查询详情
         */
        User userSelect = new User();
//        userSelect.setTelphone(telphone);
        userSelect.setUser_name("火柴");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(userSelect);
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 == null) {
            apiResponse.setResultCode(ResultCode.USERNAME_ERROR);
            return apiResponse;
        }
        System.out.println("根据useName查询的user ： " + user1.toString());
        LoggerUtil.info(user1.getUser_name(), LoginServiceimpl.class);

        Password password = new Password();
        password.setUserId(user1.getUserId());
        Password p1 = passwordMapper.selectOne(new QueryWrapper<>(password));
        if (StringUtils.equals(pad, p1.getPassword())) {
            String token = JwtUtil.sign(user1.getUserId());
            System.out.println("token ： " + token);
            //不写queryWrapper条件 会变更全部
            UpdateWrapper userUpdateWrapper = new UpdateWrapper();
            userUpdateWrapper.eq("user_id", user1.getUserId()); //不写queryWrapper条件 会变更全部
            user1.setUpdate_date("" + LocalDateTime.now());
            user1.setToken(token);
            int i = userMapper.update(user1, userUpdateWrapper);
            //将token缓存到redis中 便于查询  查询id是否存在 如果存在则先删除
//            redisUtil.set(user1.getUserId(), user1.getToken());
            if (i > 0) {
                //修改成功
                System.out.println("token ： 修改成功");
            } else {
                System.out.println("token ： 修改失败");
            }

            apiResponse.setResultCode(ResultCode.SUCCESS);
            apiResponse.setData(convertFromModel(user1));
            return apiResponse;
        } else {
            apiResponse.setResultCode(ResultCode.PASSWORD_ERROR);
            return apiResponse;
        }

    }


    //将核心模型转化为UI可用模型
    private UserVo convertFromModel(User userModel) {
        if (null == userModel) {
            return null;
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userModel, userVo);
        System.out.println("userVo ： " + userVo.toString());
        return userVo;
    }
}
